<template>
  <el-dialog title="添加角色" :visible.sync="dialog" @close="closeDialog">
    <el-form class="form-box" ref="roleFromRef" :rules="roleRules" :model="roleFrom" label-width="100px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="roleFrom.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="roleDesc">
        <el-input v-model="roleFrom.roleDesc"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialog = false">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  data () {
    return {
      dialog: false,
      roleFrom: {
        roleName: '',
        roleDesc: ''
      },
      // 输入规则验证
      roleRules: {
        roleName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹窗打开
    openDialog (data) {
      this.dialog = data[0]
    }, // 弹窗关闭
    closeDialog () {
      this.$refs.roleFromRef.resetFields()
    },
    // 添加用户
    async add () {
      this.$refs.roleFromRef.validate(async status => {
        console.log(status)
        console.log(this.roleInfo)

        if (!status) {
          return this.$message.error('请填写正确信息')
        }
        const { data: res } = await this.axios.post('roles', this.roleFrom)
        console.log(res)
        if (res.meta.status !== 201) {
          this.$message.error(res.meta.msg)
        } else {
          this.$message.success(res.meta.msg)
          // 关闭弹窗
          this.dialog = false
          // 重新获取数据
          this.$parent.getRoles()
        }
      })
    }
  }
}
</script>
